/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.repository.config;

import cn.ac.nercis.pes.repository.model.base.CustomID;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.UUIDHexGenerator;

import java.util.Objects;

/**
 * 自定义实体类主键生成策略
 * @author zpy
 * @version 1.0
 */
public class CustomUUIDGenerator extends UUIDHexGenerator {
    @Override
    public Object generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        try{
            if(object instanceof CustomID entity && StringUtils.isNotBlank(entity.getId())){
                return entity.getId();
            }else{
                return super.generate(session,object);
            }
        } catch (Exception e) {
            return super.generate(session,object);
        }
    }
}